package org.minapp.idiom;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.minapp.idiom.base.Result;
import org.minapp.idiom.module.FileUploadInfo;
import org.minapp.idiom.service.FileUploadService;
import org.nutz.boot.NbApp;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.filepool.NutFilePool;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.*;
import org.nutz.lang.random.R;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@IocBean(create="init", depose="depose")
@ChainBy(args = "config/chain/nutzwk-mvc-chain.json")
public class MainLauncher {

    @Inject
    protected PropertiesProxy conf;
    @Inject
    protected NutFilePool filePool;
    @Inject
    protected FileUploadService uploadService;


    @Inject
    protected Dao dao;

    
    @At("/")
    @Ok("->:/index.html")
    public void index() {}
    
    public void init() {
        // NB自身初始化完成后会调用这个方法
        Daos.createTablesInPackage(dao, "org.minapp.idiom", false);
        Daos.migration(dao, "org.minapp.idiom", true, false, false);
    }
    public void depose() {}

    public static void main(String[] args) throws Exception {
        new NbApp().setArgs(args).setPrintProcDoc(true).run();
    }

    @At("/upload")
    @Ok("json")
    @AdaptBy(type = UploadAdaptor.class,args = {"ioc:fileUpload"})
    public Object upload(@Param("file") TempFile[] fts){
        try{
           // List<Map<String, String>> tmpFilePaths = new ArrayList<Map<String, String>>();
            List<String> paths = new ArrayList<>();
            for (TempFile ft :fts){
                Map<String, String> map = new HashMap<>();
                String path = ft.getFile().getPath();
             //   System.out.println("===============================   "+path);
                paths.add(path);


            }

            List<FileUploadInfo> uploads = uploadService.upload(paths);
            return Result.success("fileupload.success", uploads);

        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }


    @At("/test/shiro")
    @Ok("json")
    public Object shiro(){
        return Result.success();
    }



}
